/*
The MIT License
Copyright (c) 2019 Lehrstuhl Informatik 11 - RWTH Aachen University
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE

This file is part of embeddedRTPS.

Author: i11 - Embedded Software, RWTH Aachen University
*/

#ifndef RTPS_RTPSWRITER_H
#define RTPS_RTPSWRITER_H

#include "lwip/sys.h"
#include "rtps/common/types.h"
#include "rtps/config.h"
#include "rtps/entities/Writer.h"
#include "rtps/storages/MemoryPool.h"
#include "rtps/storages/SimpleHistoryCache.h"

#define NetworkDriver struct rtps_UdpDriver
#define SimpleHistoryCache struct rtps_SimpleHistoryCache
#define CacheChange struct rtps_CacheChange

typedef struct rtps_StatelessWriter {
  struct rtps_Writer writer;
  sys_mutex_t m_mutex;
  ThreadPool *mp_threadPool/* = nullptr*/;

  rtps_PacketInfo m_packetInfo;
  rtps_UdpDriver *m_transport;
  bool m_enforceUnicast;

  TopicKind_t m_topicKind/* = TopicKind_t::NO_KEY*/;
  SequenceNumber_t m_nextSequenceNumberToSend/* = {0, 1}*/;
  struct rtps_SimpleHistoryCache *m_history;

} StatelessWriterT;
#define StatelessWriter StatelessWriterT

bool rtps_StatelessWriterT_init(struct rtps_StatelessWriter *slw, TopicData attributes, TopicKind_t topicKind, ThreadPool *threadPool,
          rtps_UdpDriver *driver, bool enfUnicast/* = false*/);

bool rtps_StatelessWriterT_addNewMatchedReader(struct rtps_StatelessWriter *slw, const ReaderProxy *newProxy);
void rtps_StatelessWriterT_removeReader(struct rtps_StatelessWriter *slw, const Guid_t *guid);
void rtps_StatelessWriterT_removeReaderOfParticipant(struct rtps_StatelessWriter *slw, const GuidPrefix_t *guidPrefix);
void rtps_StatelessWriterT_progress(struct rtps_StatelessWriter *slw);
const struct rtps_CacheChange *rtps_StatelessWriterT_newChange(struct rtps_StatelessWriter *slw, ChangeKind_t kind, const uint8_t *data,
                             DataSize_t size);
void rtps_StatelessWriterT_setAllChangesToUnsent(struct rtps_StatelessWriter *slw);
void rtps_StatelessWriterT_onNewAckNack(struct rtps_StatelessWriter *slw, const SubmessageAckNack *msg,
                  const GuidPrefix_t *sourceGuidPrefix);

bool rtps_StatelessWriterT_isIrrelevant(ChangeKind_t kind);

void rtps_StatelessWriterT_manageSendOptions();
void rtps_StatelessWriterT_resetSendOptions();

#define rtps_StatelessWriter_init rtps_StatelessWriterT_init
#define rtps_StatelessWriter_addNewMatchedReader rtps_StatelessWriterT_addNewMatchedReader
#define rtps_StatelessWriter_removeReader rtps_StatelessWriterT_removeReader
#define rtps_StatelessWriter_removeReaderOfParticipant rtps_StatelessWriterT_removeReaderOfParticipant
#define rtps_StatelessWriter_progress rtps_StatelessWriterT_progress
#define rtps_StatelessWriter_newChange rtps_StatelessWriterT_newChange
#define rtps_StatelessWriter_setAllChangesToUnsent rtps_StatelessWriterT_setAllChangesToUnsent
#define rtps_StatelessWriter_onNewAckNack rtps_StatelessWriterT_onNewAckNack
#define rtps_StatelessWriter_isIrrelevant rtps_StatelessWriterT_isIrrelevant
#define rtps_StatelessWriter_manageSendOptions rtps_StatelessWriterT_manageSendOptions
#define rtps_StatelessWriter_resetSendOptions rtps_StatelessWriterT_resetSendOptions

//using StatelessWriter = StatelessWriterT<UdpDriver>;

//#include "StatelessWriter.tpp"

#endif // RTPS_RTPSWRITER_H
